package com.noname.lib_base_java.util;


import com.noname.lib_base_java.entity.BaseSelectBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2018-09-26 11:01
 * 文件名: SelectHelper
 * 描述:
 */
public class SelectHelper {
    //全选、撤销全选选择实体列表
    public static <T extends BaseSelectBean> void selectAllSelectBeans(List<T> selectBeans, boolean select) {
        if (StrUtil.isEmpty(selectBeans)) {
            return;
        }
        for (BaseSelectBean selectBean : selectBeans) {
            selectBean.setSelect(select);
        }
    }

    //全选、撤销全选选择实体列表
    public static <T extends BaseSelectBean> void selectAllSelectBeans(T[] selectBeans, boolean select) {
        if (StrUtil.isEmpty(selectBeans)) {
            return;
        }
        for (BaseSelectBean selectBean : selectBeans) {
            selectBean.setSelect(select);
        }
    }

    //检查选择实体列表是否已经全选
    public static <T extends BaseSelectBean> boolean isSelectAll(List<T> selectBeans) {
        if (StrUtil.isEmpty(selectBeans)) {
            return false;
        }
        for (BaseSelectBean selectBean : selectBeans) {
            if (!selectBean.isSelect()) {
                return false;
            }
        }
        return true;
    }

    //检查选择实体列表是否都没选
    public static <T extends BaseSelectBean> boolean isNotSelectAll(List<T> selectBeans){
        if (StrUtil.isEmpty(selectBeans)) {
            return false;
        }
        for (BaseSelectBean selectBean:selectBeans){
            if (selectBean.isSelect()){
                return false;
            }
        }
        return true;
    }

    //获取已被选择的选择实体（单选）
    public static <T extends BaseSelectBean> T getSelectBean(List<T> selectBeans) {
        if (StrUtil.isEmpty(selectBeans)) {
            return null;
        }
        for (T selectBean : selectBeans) {
            if (selectBean.isSelect()) {
                return selectBean;
            }
        }
        return null;
    }

    //获取已被选择的选择实体（多选）
    public static <T extends BaseSelectBean> List<T> getSelectBeans(List<T> selectBeans) {
        ArrayList<T> ts = new ArrayList<>();
        if (!StrUtil.isEmpty(selectBeans)) {
            for (T selectBean : selectBeans) {
                if (selectBean.isSelect()) {
                    ts.add(selectBean);
                }
            }
        }
        return ts;
    }
}
